/*
 * @Description: 右侧的按钮、以及一些判断逻辑的信息
 */
import { defineStore } from 'pinia';
import { useSummaryCommentStore } from '../comment';
import pinia from '@/store';
import { useBaseInfoStore } from '@/store/modules/base-info';
import api from '@/api';
import {
  getBasicButtonList,
  // summaryBtnList,
  // basicButtonList,
  transformCamelback,
  codeFixed
} from './helper';

export const useNodePermissionStore = defineStore('nodePermission', {
  state: () => ({
    normalButtonList: [], //常用按钮
    advancedButtonList: [], //高级按钮
    //基本按钮。分位两部分，
    //第一部分固定几个按钮（目前明确是“提交类”按钮）居右对齐（提交-存为草稿-暂存待办-同意-不同意）。
    //第二部分就是一般的按钮
    basicButtonList: [],
    disagreeAction: [], //用户能够选择的不同意操作
    allowMap: {} //一些按钮之外的权限（打印、意见输入框、跟踪状态、常用语、上传附件、关联文档、处理后归档）
  }),
  actions: {
    //获取所有权限
    async getPermissions() {
      //判断是否加锁
      const {hasFormLock} = useBaseInfoStore().lockParam;
      const {summary:{
        inSpecialBack= false
      }} = useBaseInfoStore().businessData;
      const {
        attitudeList,
        advancedActionList,
        baseActionList,
        commonActionList,
        disagreeAction,
        dealDepartmentList,
        allowMap
      } = await api.summaryNodePermission( {
        hasFormLock: hasFormLock === true,
        isInSpecialBack: inSpecialBack === true,
      });

      //不同意的时候，用户可选操作列表------
      //大驼峰转小驼峰
      disagreeAction.forEach((d) => {
        d.codeOriginal = d.code;
        d.code = codeFixed(d.code);
      });
      this.disagreeAction = disagreeAction;
      //--------------------
      //分配按钮------------------------
      //底部按钮

      this.normalButtonList = transformCamelback(commonActionList);
      //下拉框按钮
      this.advancedButtonList = transformCamelback(advancedActionList);
      //顶部按钮
      this.basicButtonList = getBasicButtonList(transformCamelback(baseActionList));
      //--------------------------------

      const useSummaryComment = useSummaryCommentStore();
      //设置态度。节点权限是发起人填写的时候没有态度
      if (attitudeList.length) {
        useSummaryComment.setAttitudeList(attitudeList);
      }
      //设置部门
      useSummaryComment.setDealDepartmentList(dealDepartmentList);
      //设置一些按钮之外的权限
      //（打印、意见输入框、跟踪状态、常用语、上传附件、关联文档、处理后归档）
      this.allowMap = allowMap;
      const {allowTrack} = allowMap;
      //不允许跟踪，清空数据
      if (allowTrack === false) {
        useBaseInfoStore().setTrackData({});
        useSummaryComment.trackSet.isTrack = false;
      }
    }
  }
});

//在vue上下文之外使用
export function useNodePermissionWithOut() {
  return useNodePermissionStore(pinia);
}
